//
//  SNKNavTabBar.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/8.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKNavTabBar.h"

@interface SNKNavTabBar () {
    UIScrollView      *_navigationTabBar;
    UIView            *_line;
    NSArray           *_itemsWidth;
}

@end

@implementation SNKNavTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupConfig];
    }
    return self;
}

- (void)setupConfig {
    _items = [@[] mutableCopy];
    [self setupUI];
}

- (void)setupUI {
    // 留下40存放天气button
    _navigationTabBar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, SCREENW - 40, 44)];
    _navigationTabBar.backgroundColor = [UIColor clearColor];
    _navigationTabBar.showsHorizontalScrollIndicator = NO;
    [self addSubview:_navigationTabBar];
}

- (void)updateData {
    _itemsWidth = [self getButtonWidthWithTitles:_itemTitles];
    if (_itemsWidth.count) {
        CGFloat contentWidth = [self contentWidthAndAddNavTabBarItemsWithButtonWidth:_itemsWidth];
        _navigationTabBar.contentSize = CGSizeMake(contentWidth, 0);
    }
}

- (CGFloat)contentWidthAndAddNavTabBarItemsWithButtonWidth:(NSArray *)widths {
    
    CGFloat buttonX = 0;
    
    for (NSInteger index = 0; index < [_itemTitles count]; index++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_itemTitles[index] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        CGSize textMaxSize = CGSizeMake(SCREENW, MAXFLOAT);
        CGSize textRealSize = [_itemTitles[index] sizeWithFont:[UIFont systemFontOfSize:16] maxSize:textMaxSize].size;
        // label之间15的padding
        textRealSize = CGSizeMake(textRealSize.width + 15 * 2, 44);
        button.frame = CGRectMake(buttonX, 0, textRealSize.width, 44);
        
        // 字体颜色
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(itemPressed:type:) forControlEvents:UIControlEventTouchUpInside];
        [_navigationTabBar addSubview:button];
        [_items addObject:button];
        buttonX += button.kdj_width;
    }
    
    [self showLineWithButtonWidth:[widths[0] floatValue]];
    return buttonX;
}

#pragma mark - 下划线
- (void)showLineWithButtonWidth:(CGFloat)width {
    // 第一个线的位置
    _line = [[UIView alloc] initWithFrame:CGRectMake(15, 45 - 3.0f, width, 2.0f)];
    _line.backgroundColor = [UIColor redColor];
    [_navigationTabBar addSubview:_line];
    
    UIButton *btn = _items[0];
    [self itemPressed:btn type:0];
}

- (void)itemPressed:(UIButton *)button type:(NSInteger)type {
    NSInteger index = [_items indexOfObject:button];
    [_delegate itemDidSelectedWithIndex:index withCurrentIndex:_currentItemIndex];
}

// 计算数组内字体的宽度
- (NSArray *)getButtonWidthWithTitles:(NSArray *)titles {
    NSMutableArray *widths = [@[] mutableCopy];
    
    for (NSString *title in titles) {
        CGSize textMaxSize = CGSizeMake(SCREENW, MAXFLOAT);
        // 默认16的字体大小
        CGSize textRealSize = [title sizeWithFont:[UIFont systemFontOfSize:16] maxSize:textMaxSize].size;
        
        NSNumber *width = [NSNumber numberWithFloat:textRealSize.width];
        [widths addObject:width];
    }
    return widths;
}

#pragma mark - 偏移
- (void)setCurrentItemIndex:(NSInteger)currentItemIndex {
    _currentItemIndex = currentItemIndex;
    UIButton *button = _items[currentItemIndex];
    
    CGFloat flag = SCREENW - 40;
    
    if (button.kdj_x + button.kdj_width + 50 >= flag) {
        CGFloat offsetX = button.kdj_x + button.kdj_width - flag;
        if (_currentItemIndex < [_itemTitles count] - 1) {
            offsetX = offsetX + button.kdj_width;
        }
        [_navigationTabBar setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    } else {
        [_navigationTabBar setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    // 下划线的偏移量
    [UIView animateWithDuration:0.3f animations:^{
        _line.frame = CGRectMake(button.kdj_x + 15, _line.kdj_y, [_itemsWidth
                                                                  [currentItemIndex] floatValue], _line.kdj_height);
    }];
}

@end
